package mil.af.amc.adpetracker.web.actions.struts1.AdpeAccount;

import mil.af.amc.adpetracker.exceptions.IllegalUnassignmentOfPrimaryCustodianException;
import mil.af.amc.adpetracker.exceptions.UserNotFoundException;
import mil.af.amc.adpetracker.model.AdpeAccount;
import mil.af.amc.adpetracker.service.AdpeAccountService;
import mil.af.amc.adpetracker.service.ServiceException;
import mil.af.amc.adpetracker.service.exceptions.AdpeAccountNotFoundException;
import mil.af.amc.adpetracker.service.exceptions.DraNotFoundException;
import mil.af.amc.adpetracker.web.actions.struts1.AbstractDispatchAction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */

public class AdpeAccountDispatchAction extends AbstractDispatchAction {

    Log log = LogFactory.getLog(AdpeAccountDispatchAction.class);

    private AdpeAccountService adpeAccountService;

    public void setAdpeAccountService(AdpeAccountService adpeAccountService) {
        this.adpeAccountService = adpeAccountService;
    }

    public ActionForward viewAccountProfile(ActionMapping actionMapping,
                                            ActionForm actionForm,
                                            HttpServletRequest httpServletRequest,
                                            HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewAccountProfile");

    }

    public ActionForward viewAdpeAccountIndex(ActionMapping actionMapping,
                                              ActionForm actionForm,
                                              HttpServletRequest httpServletRequest,
                                              HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewAdpeAccountIndex");

    }

    public ActionForward viewCreateAdpeAccount(ActionMapping actionMapping,
                                               ActionForm actionForm,
                                               HttpServletRequest httpServletRequest,
                                               HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewCreateAdpeAccount");

    }

    public ActionForward createAdpeAccount(ActionMapping actionMapping,
                                           ActionForm actionForm,
                                           HttpServletRequest httpServletRequest,
                                           HttpServletResponse httpServletResponse) {

        DynaActionForm form = (DynaActionForm) actionForm;
        ActionMessages errors = form.validate(actionMapping, httpServletRequest);
        if (!errors.isEmpty()) {
            saveErrors(httpServletRequest, errors);
            actionMapping.findForward("viewCreateAdpeAccount");
        }

        AdpeAccount acct;
        try {
            acct = adpeAccountService.createAccount(form.getMap());
        } catch (DraNotFoundException e) {

            ActionMessage actionMessage = new ActionMessage("error.dra.notfound", form.get("draId"));
            saveError(httpServletRequest, actionMessage);

            return actionMapping.findForward("viewCreateAdpeAccount");

        }

        log.info("Created DRA " + acct.getName());
        return actionMapping.findForward("viewAdpeAccountIndex");

    }

    public ActionForward deleteAdpeAccount(ActionMapping actionMapping,
                                           ActionForm actionForm,
                                           HttpServletRequest httpServletRequest,
                                           HttpServletResponse httpServletResponse) {

        int accountId = Integer.parseInt(httpServletRequest.getParameter("adpeAccountId"));
        AdpeAccount acct = adpeAccountService.getById(accountId);

        try {
            adpeAccountService.deleteAccount(accountId);
        } catch (ServiceException e) {
            ActionMessage actionMessage = new ActionMessage(e.getKey(), e);
            saveError(httpServletRequest, actionMessage);

        }

        return actionMapping.findForward("viewAdpeAccountIndex");

    }

    public ActionForward viewAssignPrimaryCustodian(ActionMapping actionMapping,
                                                    ActionForm actionForm,
                                                    HttpServletRequest httpServletRequest,
                                                    HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewAssignPrimaryCustodian");

    }

    public ActionForward assignPrimaryCustodian(ActionMapping actionMapping,
                                                ActionForm actionForm,
                                                HttpServletRequest httpServletRequest,
                                                HttpServletResponse httpServletResponse) {

        DynaActionForm form = (DynaActionForm) actionForm;

        try {
            adpeAccountService.assignPrimaryCustodian(form.getMap());
        } catch (ServiceException e) {

            ActionMessage message = new ActionMessage(e.getKey());
            saveError(httpServletRequest, message);

        }

        return actionMapping.findForward("viewAccountProfile");
    }

    public ActionForward viewAssignAlternateCustodian(ActionMapping actionMapping,
                                                      ActionForm actionForm,
                                                      HttpServletRequest httpServletRequest,
                                                      HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewAssignAlternateCustodians");
    }

    public ActionForward assignAlternateCustodian(ActionMapping actionMapping,
                                                  ActionForm actionForm,
                                                  HttpServletRequest httpServletRequest,
                                                  HttpServletResponse httpServletResponse) {

        DynaActionForm form = (DynaActionForm) actionForm;

        try {
            adpeAccountService.assignAlternateCustodian(form.getMap());
        } catch (IllegalUnassignmentOfPrimaryCustodianException e) {
            ActionMessage actionMessage = new ActionMessage("error.adpeaccount.illegalremoveprimarycustodian", form.get("draId"));
            saveError(httpServletRequest, actionMessage);

            return actionMapping.findForward("viewAssignAlternateCustodians");
        } catch (ServiceException e) {
            ActionMessage message = new ActionMessage(e.getKey());
            saveError(httpServletRequest, message);
        }

        return actionMapping.findForward("viewAccountProfile");

    }

    public ActionForward removeAlternateCustodian(ActionMapping actionMapping,
                                                  ActionForm actionForm,
                                                  HttpServletRequest httpServletRequest,
                                                  HttpServletResponse httpServletResponse) {

        int userId = Integer.parseInt(httpServletRequest.getParameter("userId"));
        int accountId = (Integer) httpServletRequest.getSession().getAttribute("accountId");

        try {
            adpeAccountService.removeAlternateCustodian(accountId, userId);
        } catch (AdpeAccountNotFoundException e) {

            ActionMessage actionMessage = new ActionMessage("error.account.notfound", accountId);
            saveError(httpServletRequest, actionMessage);

            return actionMapping.findForward("viewRemoveAlternateCustodian");

        } catch (UserNotFoundException e) {

            ActionMessage actionMessage = new ActionMessage("error.user.notfound", accountId);
            saveError(httpServletRequest, actionMessage);

            return actionMapping.findForward("viewRemoveAlternateCustodian");

        }

        ActionMessage actionMessage = new ActionMessage("messages.account.removealtcustodian");
        saveError(httpServletRequest, actionMessage);
        return actionMapping.findForward("viewAccountProfile");

    }
}
